package com.smart.smartdata;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.ResultReceiver;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import java.math.BigDecimal;


public class AppUtils {
    private static Handler handler;
    private static String versionName;
    private final static long MIN_CLICK_DELAY_TIME = 1000;
    private static long lastClickTime;

    public static String getVersionName(Context context) {
        if (!TextUtils.isEmpty(versionName))
            return versionName;
        try {
            versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

    public static int getVersionCode(Context context) {
        try {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return 0;
    }



    public static void jump(Context context, Class<? extends Activity> targetActivity, Bundle bundle, int... flags) {
        long currentClickTime = System.currentTimeMillis();
        if (currentClickTime - lastClickTime >= MIN_CLICK_DELAY_TIME) {
            lastClickTime = currentClickTime;
            if (context == null || ((context instanceof Activity) && ((Activity) context).isFinishing()))
                return;
            Intent intent = new Intent(context, targetActivity);
            if (flags != null && flags.length > 0) {
                for (int flag : flags)
                    intent.addFlags(flag);
            }
            if (bundle != null)
                intent.putExtras(bundle);
            context.startActivity(intent);
        }
    }

    public static void jump(Context context, Class<? extends Activity> targetActivity, Bundle bundle) {
        jump(context, targetActivity, bundle, null);
    }

    public static void jump(Context context, Class<? extends Activity> targetActivity) {
        jump(context, targetActivity, null);
    }

    public static void toastShort(final Context context, final String message) {
        if (handler == null)
            handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
            }
        });
    }


    /**
     * @param activity
     * @param resultReceiver
     */
    public static void hideSoftInput(Activity activity, ResultReceiver resultReceiver) {
        if (activity == null)
            return;
        InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0, resultReceiver);
    }

    /**
     * 隐藏键盘
     *
     * @param
     */
    public static void hideSoftInput(Activity activity) {
        hideSoftInput(activity, null);
    }

    public static void hideSoftInput(View view) {
        hideSoftInput(view, null);
    }

    public static void hideSoftInput(View view, ResultReceiver resultReceiver) {
        if (view == null)
            return;
        InputMethodManager inputMethodManager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0, resultReceiver);
    }

    /**
     * 显示键盘
     */
    public static void showSoftInput(View view) {
        InputMethodManager inputMethodManager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.showSoftInput(view, 0, null);
    }

    /**
     * @param view
     * @param resultReceiver
     */
    public static void showSoftInput(View view, ResultReceiver resultReceiver) {
        InputMethodManager inputMethodManager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.showSoftInput(view, 0, resultReceiver);
    }

    public static Float removePoint(Float f) {
        if(f>=100){
            BigDecimal b = new BigDecimal(f);
            return b.setScale(0, BigDecimal.ROUND_HALF_UP).floatValue();
        }else if(f>=10){
            BigDecimal b = new BigDecimal(f);
            return b.setScale(1, BigDecimal.ROUND_HALF_UP).floatValue();
        }else{
            BigDecimal b = new BigDecimal(f);
            return b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
        }
    }
}
